﻿define(["jquery", "jqueryValid", "jqueryValidMethods"], function ($, jqV, jqueryValidMethods) {
    var isExist = false;
    return {
        init: function (isListPage) {
            $(".manger-con:not(.manger-con2)").on("hidden.bs.modal", function () {
                $(this).removeData("bs.modal").find(".modal-content").empty();
            });

            $("#createForm").validate({
                submitHandler: function () {
                    $("#btnCreate").attr({ "disabled": "disabled" });
                    if (!isExist) {
                        $(".red-error").html("");
                        return true;
                    }
                    else {
                        $("#btnCreate").removeAttr("disabled");
                        $(".red-error").html("同一个系统、事件、动作、动作参数下只能有一个规则。");
                        return false;
                    }
                },
                rules: {
                    RuleNo: {
                        required: true
                    },
                    RuleName: {
                        required: true,
                        isEmpty: true,
                        maxlength: 50,
                        remote: {
                            type: "post",
                            url: "/Rule/ValidateRuleName",
                            data: {
                                name: function () {
                                    return $("#RuleName").val();
                                },
                                id: 0
                            },
                            dataType: "json",
                            dataFilter: function (data, type) {
                                var dataObj = eval("(" + data + ")");
                                if (!dataObj.exist)
                                    return true;
                                else
                                    return false;
                            }
                        }
                    },
                    ShortEnableTime: {
                        required: true
                    },
                    AppId: {
                        min: 1
                    },
                    EventId: {
                        min: 1
                    },
                    ActionId: {
                        min: 1
                    },
                    ActionPoint: {
                        required: true,
                        integer: true,
                        min: 1,
                        max: 1000
                    },
                    BalanceLimitType: {
                        required: true,
                        integer2: true
                    },
                    Memo: {
                        maxlength: 255
                    }
                },
                messages: {
                    RuleNo: {
                        required: "规则编号不可为空"
                    },
                    RuleName: {
                        required: "规则名称不可为空",
                        isEmpty: "不能输入空格",
                        maxlength: "规则名称不能超过50个字符",
                        remote: "您输入的规则名称已存在"
                    },
                    ShortEnableTime: {
                        required: "规则生效时间不可为空"
                    },
                    AppId: {
                        min: "请选择业务系统"
                    },
                    EventId: {
                        min: "请选择事件"
                    },
                    ActionId: {
                        min: "请选择动作"
                    },
                    ActionPoint: {
                        required: "请输入1到1000的整数",
                        integer: "请输入1到1000的整数",
                        min: "请输入1到1000的整数",
                        max: "请输入1到1000的整数"
                    },
                    BalanceLimitType: {
                        required: "请输入整数",
                        integer2: "请输入整数"
                    },
                    Memo: {
                        maxlength: "规则说明不能超过255个字符"
                    }
                }
            });

            $("#editForm").validate({
                submitHandler: function () {
                    $("#btnCreate").attr({ "disabled": "disabled" });
                    if (!isExist) {
                        $(".red-error").html("");
                        return true;
                    }
                    else {
                        $("#btnCreate").removeAttr("disabled");
                        $(".red-error").html("同一个系统、事件、动作、动作参数下只能有一个规则。");
                        return false;
                    }
                },
                rules: {
                    RuleNo: {
                        required: true
                    },
                    RuleName: {
                        required: true,
                        isEmpty: true,
                        maxlength: 50,
                        remote: {
                            type: "post",
                            url: "/Rule/ValidateRuleName",
                            data: {
                                name: function () {
                                    return $("#RuleName").val();
                                },
                                id: parseInt($("#Id").val())
                            },
                            dataType: "json",
                            dataFilter: function (data, type) {
                                var dataObj = eval("(" + data + ")");
                                if (!dataObj.exist)
                                    return true;
                                else
                                    return false;
                            }
                        }
                    },
                    ShortEnableTime: {
                        required: true
                    },
                    AppId: {
                        min: 1
                    },
                    EventId: {
                        min: 1
                    },
                    ActionId: {
                        min: 1
                    },
                    ActionPoint: {
                        required: true,
                        integer: true,
                        min: 1,
                        max: 1000
                    },
                    BalanceLimitType: {
                        required: true,
                        integer2: true
                    },
                    Memo: {
                        maxlength: 255
                    }
                },
                messages: {
                    RuleNo: {
                        required: "规则编号不可为空"
                    },
                    RuleName: {
                        required: "规则名称不可为空",
                        isEmpty: "不能输入空格",
                        maxlength: "规则名称不能超过50个字符",
                        remote: "您输入的规则名称已存在"
                    },
                    ShortEnableTime: {
                        required: "规则生效时间不可为空"
                    },
                    AppId: {
                        min: "请选择业务系统"
                    },
                    EventId: {
                        min: "请选择事件"
                    },
                    ActionId: {
                        min: "请选择动作"
                    },
                    ActionPoint: {
                        required: "请输入1到1000的整数",
                        integer: "请输入1到1000的整数",
                        min: "请输入1到1000的整数",
                        max: "请输入1到1000的整数"
                    },
                    BalanceLimitType: {
                        required: "请输入整数",
                        integer2: "请输入整数"
                    },
                    Memo: {
                        maxlength: "规则说明不能超过255个字符"
                    }
                }
            });

            $("#publishingForm").validate({
                submitHandler: function () {
                    $("#btnCreate").attr({ "disabled": "disabled" });
                    return true;
                },
                rules: {
                    EnableTime: {
                        required: true,
                        compareDate2: true
                    }
                },
                messages: {
                    EnableTime: {
                        required: "生效时间不可为空",
                        compareDate2: "生效时间必须大于当前时间"
                    }
                }
            });

            $("#AppId").unbind("change").bind("change", function () {
                var $this = $(this);
                var appId = parseInt($this.val());
                if (appId == 0) {
                    //重置
                    $("#EventId").empty();
                    $('#EventId').append("<option value='0'>请选择</option>");
                    $("#ActionId").empty();
                    $('#ActionId').append("<option value='0'>请选择</option>");
                    $("#ParamId").empty();
                    $('#ParamId').append("<option value='0'>请选择</option>");
                    return;
                }
                $.ajax({
                    url: '/Rule/GetEventsAjax/',
                    cache: false,
                    type: 'post',
                    data: { 'appId': appId },
                    success: function (result) {
                        $("#EventId").empty();
                        $('#EventId').append("<option value='0'>请选择</option>");
                        $.each(result.events, function (index, value) {
                            $('#EventId').append("<option value='" + value.id + "'>" + value.name + "</option>");
                        });

                        //重置
                        $("#ActionId").empty();
                        $('#ActionId').append("<option value='0'>请选择</option>");
                        if (!isListPage) {
                            $("#ParamId").empty();
                            $('#ParamId').append("<option value='0'>请选择</option>");
                        }
                    }
                });
            });

            $("#EventId").unbind("change").bind("change", function () {
                var $this = $(this);
                var eventId = parseInt($this.val());
                if (eventId == 0) {
                    //重置
                    $("#ActionId").empty();
                    $('#ActionId').append("<option value='0'>请选择</option>");
                    $("#ParamId").empty();
                    $('#ParamId').append("<option value='0'>请选择</option>");
                    return;
                }
                $.ajax({
                    url: '/Rule/GetActionsAjax/',
                    cache: false,
                    type: 'post',
                    data: { 'eventId': eventId },
                    success: function (result) {
                        $("#ActionId").empty();
                        $('#ActionId').append("<option value='0'>请选择</option>");
                        $.each(result.actions, function (index, value) {
                            $('#ActionId').append("<option value='" + value.id + "'>" + value.name + "</option>");
                        });
                        if (!isListPage) {
                            //重置
                            $("#ParamId").empty();
                            $('#ParamId').append("<option value='0'>请选择</option>");
                        }
                    }
                });
            });

            $("#ActionId").unbind("change").bind("change", function () {
                var $this = $(this);
                var actionId = parseInt($this.val());
                if (actionId == 0) {
                    //重置
                    $("#ParamId").empty();
                    $('#ParamId').append("<option value='0'>请选择</option>");
                    return;
                }
                $.ajax({
                    url: '/Rule/GetParamsAjax/',
                    cache: false,
                    type: 'post',
                    data: { 'actionId': actionId },
                    success: function (result) {
                        $("#ParamId").empty();
                        $('#ParamId').append("<option value='0'>请选择</option>");
                        $.each(result.paramInfos, function (index, value) {
                            $('#ParamId').append("<option value='" + value.id + "'>" + value.name + "</option>");
                        });
                    }
                });
                if (!isListPage) {
                    //判断是否存在
                    $.ajax({
                        url: '/Rule/ValidateRule/',
                        cache: false,
                        type: 'post',
                        data: { 'appId': $("#AppId").val(), 'eventId': $("#EventId").val(), 'actionId': $("#ActionId").val(), 'paramId': $("#ParamId").val(), 'id': $("#Id").val() },
                        success: function (result) {
                            isExist = result.exist;
                        }
                    });
                }
            });

            $("#ParamId").unbind("change").bind("change", function () {
                //判断是否存在
                $.ajax({
                    url: '/Rule/ValidateRule/',
                    cache: false,
                    type: 'post',
                    data: { 'appId': $("#AppId").val(), 'eventId': $("#EventId").val(), 'actionId': $("#ActionId").val(), 'paramId': $("#ParamId").val(), 'id': $("#Id").val() },
                    success: function (result) {
                        isExist = result.exist;
                    }
                });
            });

            $("#btnCreateCopy").unbind("click").bind("click", function () {
                if ($('#editForm').valid()) {
                    if (!isExist) {
                        $(".red-error").html("");
                        if ($("#ShortEnableTime").val() != $("#ShortEnableTime2").val()) {
                            $("#EnableTime").val($("#ShortEnableTime").val() + " 00:00:00");
                        }
                        $("#EnableTime").rules("add", {
                            required: true,
                            compareDate2: true,
                            compareDate3: "#EnableTime2",
                            messages: {
                                required: "生效时间不可为空",
                                compareDate2: "生效时间必须大于当前时间",
                                compareDate3: "生效时间必须大于上次生效时间"
                            }
                        });
                        $("#dataPart").modal();
                    }
                    else {
                        $(".red-error").html("同一个系统、事件、动作、动作参数下只能有一个规则。");
                    }
                }
                return false;
            });

            $("#createForm #btnCancel").unbind("click").bind("click", function () {
                //重置
                $("#EventId").empty();
                $('#EventId').append("<option value='0'>请选择</option>");
                $("#ActionId").empty();
                $('#ActionId').append("<option value='0'>请选择</option>");
                $("#ParamId").empty();
                $('#ParamId').append("<option value='0'>请选择</option>");
            });

            $("#editForm #btnCancel").unbind("click").bind("click", function () {
                //重置
                isExist = false;
                $('#AppId').html(window.AppIdHtml);
                $('#EventId').html(window.EventIdHtml);
                $('#ActionId').html(window.ActionIdHtml);
                $('#ParamId').html(window.ParamIdHtml);
                $('#AppId').val(window.AppId);
                $('#EventId').val(window.EventId);
                $('#ActionId').val(window.ActionId);
                $('#ParamId').val(window.ParamId);
            });
        }
    };
});